package com.shcz.migu.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.shcz.migu.R;


/**
 * 失败或者内容为空时显示，可配置高度
 *
 */
public class FailureFragment extends Fragment implements View.OnClickListener {

    public final static String ARGUMENT_ICON_ID     = "FailureFragment_IconResId";
    public final static String ARGUMENT_CONTENT     = "FailureFragment_Content";
    public final static String ARGUMENT_BUTTON     = "FailureFragment_Button";
    public final static String ARGUMENT_VIEW_HEIGHT     = "FailureFragment_Height";

    private FailureOnClickListener mFailureOnClickListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_list_empty, container, false);

        Bundle bundle = getArguments();

        if(bundle == null){
            view.setOnClickListener(this);
            return view;
        }

        int iconResId = bundle.getInt(ARGUMENT_ICON_ID);
        if(iconResId > 0){
            ImageView icon = (ImageView) view.findViewById(R.id.list_empty_image);
            icon.setImageResource(iconResId);
        }else if(iconResId == -1){
            view.findViewById(R.id.list_empty_image).setVisibility(View.GONE);
        }

        String content = bundle.getString(ARGUMENT_CONTENT);
        if(!TextUtils.isEmpty(content)){
            TextView textView = (TextView) view.findViewById(R.id.list_empty_text);
            textView.setText(content);
        }


        int buttonResId = bundle.getInt(ARGUMENT_BUTTON);
        if(buttonResId > 0){
            Button button = (Button) view.findViewById(R.id.list_empty_btn);
            button.setVisibility(View.VISIBLE);
            button.setText(buttonResId);
            button.setOnClickListener(this);
        } else {
            view.setOnClickListener(this);
        }

        int height = bundle.getInt(ARGUMENT_VIEW_HEIGHT);
        if(height > 0){
            view.setMinimumHeight(height);
        }

        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        if(context instanceof FailureOnClickListener){
            mFailureOnClickListener = (FailureOnClickListener) context;
        }
    }

    @Override
    public void onClick(View v) {
        if(mFailureOnClickListener != null){
            if(v.getId() == R.id.list_empty_btn){
                mFailureOnClickListener.onRefresh(true);
            }
            else{
                mFailureOnClickListener.onRefresh(false);
            }
        }
    }

    public interface FailureOnClickListener {
        void onRefresh(boolean isBtnClick);
    }

}

